package com.dongdongshop.service.impl;

import com.dongdongshop.mapper.TbBrandMapper;
import com.dongdongshop.model.TbBrand;
import com.dongdongshop.model.TbBrandExample;
import com.dongdongshop.page.PageParms;
import com.dongdongshop.page.PageUtil;
import com.dongdongshop.service.BrandService;
import com.dongdongshop.vo.BrandVO;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;
import java.util.stream.Collectors;

@Service
public class BrandServiceImpl implements BrandService {
    @Resource
    private TbBrandMapper tbBrandMapper;

    @Override
    public PageUtil<BrandVO> getBrandList(PageParms<BrandVO> parms) {
        Page<Object> objects = PageHelper.startPage(parms.getPageNumber(), parms.getPageSize());
        TbBrandExample tbBrandExample = new TbBrandExample();
        TbBrandExample.Criteria criteria = tbBrandExample.createCriteria();
        if (StringUtils.isNotBlank(parms.getParms().getFirstChar())) {
            criteria.andFirstCharEqualTo(parms.getParms().getFirstChar());
        }
        if (StringUtils.isNotBlank(parms.getParms().getName())) {
            criteria.andNameLike("%" + parms.getParms().getName() + "%");
        }
        List<TbBrand> tbBrands = tbBrandMapper.selectByExample(tbBrandExample);
        List<BrandVO> voList = tbBrands.stream().map(brand -> {
            BrandVO vo = new BrandVO();
            BeanUtils.copyProperties(brand, vo);
            return vo;
        }).collect(Collectors.toList());
        long total = objects.getTotal();
        PageInfo<BrandVO> pageInfo = new PageInfo<>(voList);
        pageInfo.setTotal(total);
        PageUtil pageUtil = new PageUtil().setTotal(pageInfo.getTotal()).setPageNumber(parms.getPageNumber()).setPageSize(parms.getPageSize()).setData(pageInfo.getList());
        pageUtil.setTotalPage(pageUtil.getTotal() % pageUtil.getPageSize() == 0 ? pageUtil.getTotal() / pageUtil.getPageSize() : (pageUtil.getTotal() / pageUtil.getPageSize()) + 1 );
        return pageUtil;

    }

    @Override
    public void addBrand(BrandVO brandVO) {
        TbBrand tbBrand = new TbBrand();
        BeanUtils.copyProperties(brandVO, tbBrand);
        tbBrandMapper.insertSelective(tbBrand);
    }

    @Override
    public BrandVO getBrankById(Long id) {
        TbBrand tbBrand = tbBrandMapper.selectByPrimaryKey(id);
        BrandVO brandVO = new BrandVO();
        BeanUtils.copyProperties(tbBrand, brandVO);
        return brandVO;
    }

    @Override
    public void updateBrand(BrandVO brandVO) {
        TbBrand tbBrand = new TbBrand();
        BeanUtils.copyProperties(brandVO, tbBrand);
        tbBrandMapper.updateByPrimaryKeySelective(tbBrand);
    }

    @Override
    public void deleteBatch(List<Long> ids) {
        TbBrandExample tbBrandExample = new TbBrandExample();
        TbBrandExample.Criteria criteria = tbBrandExample.createCriteria();
        criteria.andIdIn(ids);
        tbBrandMapper.deleteByExample(tbBrandExample);
    }

    @Override
    public List<BrandVO> getAllBrandList() {
        List<TbBrand> tbBrands = tbBrandMapper.selectByExample(null);
        List<BrandVO> voList = tbBrands.stream().map(brand -> {
            BrandVO vo = new BrandVO();
            BeanUtils.copyProperties(brand, vo);
            return vo;

        }).collect(Collectors.toList());
        return voList;
    }
}
